/*
 * @Description:数据库结构定义
 * @Author: maskMan
 * @LastEditTime: 2025-03-07 10:39:25
 */
import { NAdmin } from "../types/admin";
import { NTarget, Report } from "../types/share";
import { NDefine } from "../types/define";
//Schema列表类型
export type SchemaList<T> = Partial<Record<Table, T>>;
export type TableName = "t_admin" | "t_agent" | "t_user" | "t_game" | "t_record" | "t_task" | "t_prop" | "t_shop" | "t_actionRecord" | "t_shopRecord";
/**数据表 */
export enum Table {
  /*管理员表*/
  admin = "t_admin",
  /**代理表 */
  agent = "t_agent",
  /**用户表 */
  user = "t_user",
  /**游戏表 */
  game = "t_game",
  /**成绩记录表*/
  targetRecord = "t_targetRecord",
  /**任务表 */
  task = "t_task",
  /**道具表 */
  prop = "t_prop",
  /**商店表 */
  shop = "t_shop",
  /**购买记录 */
  shopRecord = "t_shopRecord",
  //操作记录
  actionRecord = "t_actionRecord",
  /**参数调整 */
  weight = "t_weight"
}
/**数据类型判断*/
export type Istruct<T extends Table> = T extends Table.user
  ? Partial<TUser>
  : T extends Table.admin
  ? Partial<TAdmin>
  : T extends Table.task
  ? Partial<TTask>
  : T extends Table.shop
  ? Partial<TShopRecord>
  : T extends Table.shopRecord
  ? Partial<TShop>
  : T extends Table.actionRecord
  ? Partial<TActionRecord>
  : T extends Table.agent
  ? Partial<TAgent>
  : T extends Table.targetRecord
  ? Partial<TTargetRecord>
  : never;
/**命令1 */
type TCond1<T> = {
  $and?: TCond<T>[];
  $or?: TCond<T>[];
  $in?: TCond<T>[];
};
/**命令条件 */
type TCond<T> =
  | {
      [K in keyof T]?: { $elemMatch?: T[K]; $gt?: T[K]; $eq?: T[K]; $gte?: T[K]; $lte?: T[K]; $ne?: T[K]; $not?: T[K]; $all?: [] } | T[K];
    }
  | TCond1<T>;
export type TCondition<T extends Table> = TCond<Istruct<T>>;
//排序
export type ISort<T> = {
  sort: {
    [key in keyof T]: 1 | -1; // 支持任意字段的排序，值为 1 或 -1
  };
};
//多用户更新
export type BulkOperations<T extends Table> = {
  condition: TCondition<T>;
  doc: Istruct<T>;
};
/**数据架构 */
export const structList = {
  /**用户 */
  [Table.user]: {
    UID: { type: Number, inc: true },
    account: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    name: { type: String, required: true },
    age: { type: Number, required: true },
    referrer: { type: String },
    parent: { type: String, required: true },
    vipLevel: { type: String, required: true },
    coins: { type: Number, required: true },
    points: { type: Number, required: true },
    address: { type: String },
    ipone: { type: String },
    loginCount: { type: Number, required: true },
    regTime: { type: String, required: true },
    endTime: { type: String },
    lastTime: { type: String },
    commit: { type: String },
    status: { type: Boolean, required: true }
  },
  /**任务 */
  [Table.task]: {
    TID: { type: Number, inc: true },
    account: { type: String, required: true, unique: true },
    createData: { type: Object, required: true },
    taskList: { type: Array, required: true },
    itemData: { type: Object, required: true }
  },
  /**管理员 */
  [Table.admin]: {
    AID: { type: Number, inc: true },
    account: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    name: String,
    ident: { type: String, required: true },
    ipone: String,
    avaurl: String,
    parent: String,
    roles: Object,
    //注册时间
    regTime: String,
    //最后登录时间
    lastTime: String,
    status: Boolean
  },
  /**报告记录 */
  [Table.targetRecord]: {
    RID: { type: Number, inc: true },
    //账号
    account: { type: String, required: true },
    //训练记录
    recordMaps: { type: Object, required: true },
    //计划周期
    planCount: { type: Number, required: true },
    //训练用时
    costTime: { type: String, required: true },
    //计划时间
    planTime: { type: String, required: true },
    //项目个数
    itemCount: { type: Number, required: true },
    //达标率
    fillRate: { type: String, required: true },
    //综合得分
    score: { type: Number, required: true },
    //日期
    date: { type: String, required: true }
  },
  /**商店物品 */
  [Table.shop]: {
    SID: { type: Number, inc: true },
    groupType: { type: String, required: true },
    name: { type: String, required: true },
    price: { type: Number, required: true },
    points: { type: Number, required: true },
    stock: { type: Number, required: true },
    desc: { type: String, required: true },
    detail: { type: String, required: true },
    status: { type: Boolean, required: true }
  },
  /**购买记录 */
  [Table.shopRecord]: {
    SRID: { type: Number, inc: true },
    shopId: { type: Number, required: true },
    shopName: { type: String, required: true },
    account: { type: String, required: true },
    timeStamp: { type: Number, required: true },
    name: { type: String, required: true },
    ipone: { type: String, required: true },
    address: { type: String, required: true },
    status: { type: String, required: true },
    orderIndex: String,
    commit: String
  },
  /**操作记录 */
  [Table.actionRecord]: {
    PID: { type: Number, inc: true },
    account: { type: String, required: true },
    timeStamp: { type: Number, required: true },
    actionType: { type: String, required: true },
    creator: { type: String, required: true },
    commit: { type: String, required: true }
  },
  /**参数调整 */
  [Table.weight]: {
    WID: { type: Number, required: true },
    timeBase1: { type: Number, required: true },
    timeBase2: { type: Number, required: true },
    scoreBase3: { type: Number, required: true },
    scoreRatio: { type: Number, required: true },
    coinsRatio: { type: Number, required: true },
    pointRatio: { type: Number, required: true }
  }
};
/**用户 */
export type TUser = {
  //monid
  $inc?: any;
  _id?: Object | number | string;
  //字段递增ID
  UID?: number;
  //用户账号
  account: string;
  //用户密码
  password: string;
  //真实名字
  name: string;
  //用户年龄
  age: number;
  parent: string;
  commit: string;
  //推荐人
  referrer?: string;
  vipLevel: NDefine.VipLevel;
  coins: number;
  points: number;
  address: string;
  ipone: string;
  loginCount: number;
  regTime: string;
  endTime: string;
  lastTime: string;
  status: boolean;
};
//管理员
export type TAdmin = {
  //monid
  _id?: Object | number | string;
  //字段递增ID
  AID?: number;
  //身份
  ident: NAdmin.Ident;
  //管理账号
  account: string;
  //用户密码
  password: string;
  //管理员名字
  name: string;
  //角色权限
  roles: NDefine.Obj;
  //电话号码
  ipone?: string;
  //头像
  avaurl?: string;
  //父亲
  parent?: string;
  //注册时间
  regTime: string;
  //最后登录时间
  lastTime: string;
  status: boolean;
};
export type TTask = {
  _id?: Object | number | string;
  //字段递增ID
  TID?: number;
  account: string;
  createData: NTarget.Create;
  taskList: NTarget.Task[];
  itemData: NTarget.ItemData;
};
export type TShop = {
  _id?: Object | number | string;
  //字段递增ID
  SID?: number;
  groupType: string;
  name: string;
  price: number;
  points: number;
  stock: number;
  desc: string;
  detail: string;
  status: boolean;
};
export type TShopRecord = {
  _id?: Object | number | string;
  //字段递增ID
  SRID?: number;
  shopId: number;
  shopName: string;
  account: string;
  timeStamp: number;
  name: string;
  ipone: string;
  address: string;
  status: string;
  orderIndex?: string;
  commit?: string;
};
export type TActionRecord = {
  _id?: Object | number | string;
  //字段递增ID
  PID?: number;
  account: string;
  timeStamp?: Number;
  actionType: NDefine.ActionType;
  creator: string;
  commit: string;
};
export type TAgent = {
  uid: number;
  account: string;
};
export type TTargetRecord = {
  _id?: Object | number | string;
  //字段递增ID
  RID?: number;
  account: string;
  //训练记录
  recordMaps: Report.TargetRecordMaps;
  //计划周期
  planCount: number;
  //训练用时
  costTime: string;
  //计划时间
  planTime: string;
  //项目个数
  itemCount: number;
  //达标率
  fillRate: string;
  //综合得分
  score: number;
  //日期
  date: string;
};
export type TWeight = {
  _id?: Object | number | string;
  //字段
  WID?: number;
  timeBase1: number;
  timeBase2: number;
  scoreBase3: number;
  scoreRatio: number;
  coinsRatio: number;
  pointRatio: number;
};
